﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ThreadViewer
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            concreteTest.mineParameters = parameterschecked.Items;
            concreteTest.mineTypes = minechecked.Items;
            concreteTest.threadingTypes = threadingchecked.Items;
            concreteTest.securityTypes = securitychecked.Items;

            for (int i = 0; i < 400; i++)
            {
                PBs.Add(new ProgressBar());
                LBs.Add(new Label());
                PBs[i].Parent = groupBox4;
                PBs[i].Width = 300;
                PBs[i].Visible = false;
                PBs[i].Top = i * 60 + 30;
                LBs[i].Parent = groupBox4;
                LBs[i].Top = i * 60 + 15;
                LBs[i].AutoSize = true;
                LBs[i].Visible = false;
            }
            /*dataGrid.ColumnCount = 20; 
            dataGrid.RowCount = 20;
            dataGrid.Rows[19].Cells[19].Value = "fasza";*/
        }

        List<testResult> tesztek = new List<testResult>();//ez alapján táblázatkitöltés

        private void feldolgoz(string fileName)
        {
            try
            {
                StreamReader sr1 = new StreamReader(fileName);
                tesztek.Add(new testResult(sr1));
                sr1.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Hiba!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            istenfuggveny();
        }
        List<testResult> helyesek;
        List<ProgressBar> PBs = new List<ProgressBar>();
        List<Label> LBs = new List<Label>();
        private void istenfuggveny()
        {

            helyesek = new List<testResult>();//ez alapján táblázatkitöltés; //konkrét tesztek a sorokba!
            for (int i = 0; i < tesztek.Count; i++)
            {
                if (openedchecked.GetItemChecked(i))
                {
                    helyesek.Add(new testResult(tesztek[i]));
                    for (int j = 0; j < tesztek[i].lista.Count; j++)
                    {
                        for (int a = 0; a < threadingchecked.Items.Count; a++)
                        {
                            for (int b = 0; b < securitychecked.Items.Count; b++)
                            {
                                for (int c = 0; c < minechecked.Items.Count; c++)
                                {
                                    for (int d = 0; d < parameterschecked.Items.Count; d++)
                                    {
                                        if (threadingchecked.GetItemChecked(a) && securitychecked.GetItemChecked(b) && minechecked.GetItemChecked(c) && parameterschecked.GetItemChecked(d))
                                        {
                                            if (tesztek[i].lista[j].stimmel((string)parameterschecked.Items[d], (string)threadingchecked.Items[a], (string)minechecked.Items[c], (string)securitychecked.Items[b]))
                                            {
                                                helyesek[helyesek.Count - 1].lista.Add(tesztek[i].lista[j]);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            dataGrid.RowCount = helyesek.Count == 0 ? 0 : helyesek[0].lista.Count; //1 a léceknek többi konkrét teszteset
            dataGrid.ColumnCount = 4 + helyesek.Count; //1 a teszteknek fájlból, 4 a tulajdonságoknak(mine,safe,thread,param) és x cpu
            //header egy sorban:
            for (int i = 0; i < dataGrid.ColumnCount; i++)
            {
                switch (i)
                {
                    case (0):
                        dataGrid.Columns[0].HeaderCell.Value = "Threading";
                        // dataGrid.Rows[0].Cells[i].Value = "Threading";
                        break;
                    case (1):
                        dataGrid.Columns[1].HeaderCell.Value = "Security";
                        //  dataGrid.Rows[0].Cells[i].Value = "Security";
                        break;
                    case (2):
                        dataGrid.Columns[2].HeaderCell.Value = "Mine";
                        // dataGrid.Rows[0].Cells[i].Value = "Mine";
                        break;
                    case (3):
                        dataGrid.Columns[3].HeaderCell.Value = "Parameter";
                        // dataGrid.Rows[0].Cells[i].Value = "Parameter";
                        break;
                    default:
                        dataGrid.Columns[i].HeaderCell.Value = tesztek[i - 4].procName;
                        // dataGrid.Rows[0].Cells[i].Value = tesztek[i - 5].procName;

                        break;
                }
            }
            for (int i = 0; i < 4; i++)
            {
                dataGrid.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                for (int j = 0; j < dataGrid.RowCount; j++)
                {
                    switch (i)
                    {
                        case (0):
                            dataGrid.Rows[j].Cells[i].Value = helyesek[0].lista[j].threadingType;
                            break;
                        case (1):
                            dataGrid.Rows[j].Cells[i].Value = helyesek[0].lista[j].securityType;
                            break;
                        case (2):
                            dataGrid.Rows[j].Cells[i].Value = helyesek[0].lista[j].mineType;
                            break;
                        case (3):
                            dataGrid.Rows[j].Cells[i].Value = helyesek[0].lista[j].mineParameter;
                            break;
                    }
                }
            }

            for (int i = 4; i < dataGrid.ColumnCount; i++)
            {
                dataGrid.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                for (int j = 0; j < dataGrid.RowCount; j++)
                {
                    dataGrid.Rows[j].Cells[i].Value = helyesek[i - 4].lista[j].time;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                openedchecked.Items.Add(openFileDialog1.FileName, CheckState.Checked);
                feldolgoz(openFileDialog1.FileName);
            }
        }

        private void kilépésToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void threadingchecked_SelectedValueChanged(object sender, MouseEventArgs e)
        {
            istenfuggveny();
        }

        private void securitychecked_MouseClick(object sender, MouseEventArgs e)
        {
            istenfuggveny();
        }

        private void minechecked_MouseClick(object sender, MouseEventArgs e)
        {
            istenfuggveny();
        }

        private void parameterschecked_MouseClick(object sender, MouseEventArgs e)
        {
            istenfuggveny();
        }

        private void openedchecked_MouseClick(object sender, MouseEventArgs e)
        {
            istenfuggveny();
        }

        private void threadingchecked_SelectedValueChanged(object sender, EventArgs e)
        {
            istenfuggveny();
        }

        private void securitychecked_SelectedValueChanged(object sender, EventArgs e)
        {
            istenfuggveny();
        }

        private void minechecked_SelectedValueChanged(object sender, EventArgs e)
        {
            istenfuggveny();
        }

        private void parameterschecked_SelectedValueChanged(object sender, EventArgs e)
        {
            istenfuggveny();
        }

        private void openedchecked_SelectedValueChanged(object sender, EventArgs e)
        {
            istenfuggveny();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] A = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach (string s in A)
                {
                    if (s.Length > 3 && s.Substring(s.Length - 4) == ".txt")
                    {
                        openedchecked.Items.Add(s, CheckState.Checked);
                        feldolgoz(s);
                    }
                }
            }
        }




        private void dataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                groupBox4.Text = "";
                for (int i = 0; i < 4; i++)
                {
                    groupBox4.Text += dataGrid.Rows[e.RowIndex].Cells[i].Value.ToString() + " ";

                }
                List<double> D = new List<double>();
                for (int i = 0; i < LBs.Count; i++)
                {
                    LBs[i].Visible = false;
                    PBs[i].Visible = false;
                    PBs[i].Width = groupBox4.Width - 20;
                }
                int q = 0;
                foreach (testResult tr in helyesek)
                {
                    D.Add(double.Parse(tr.lista[e.RowIndex].time.Substring(0, tr.lista[e.RowIndex].time.Length - 2), System.Globalization.CultureInfo.InvariantCulture));

                    LBs[q].Text = tr.procName + " " + tr.lista[e.RowIndex].time;
                    LBs[q].Visible = true;
                    q++;
                }
                double d = 0;
                foreach (double t in D)
                {
                    d = Math.Max(d, t);
                }
                q = 0;
                foreach (double t in D)
                {
                    PBs[q].Value = (int)(t * 100 / d);
                    PBs[q].Visible = true;
                    q++;
                }


                if (e.ColumnIndex > 3)
                {
                    adatDialog a = new adatDialog();
                    a.setText(helyesek[e.ColumnIndex - 4].lista[e.RowIndex].data.ToArray());
                    a.Show();
                }
            }
            catch
            {
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                try
                {
                    groupBox4.Text = "";
                    groupBox4.Text += tesztek[0].procName;
                    List<double> D = new List<double>();
                    for (int i = 0; i < LBs.Count; i++)
                    {
                        LBs[i].Visible = false;
                        PBs[i].Visible = false;
                        PBs[i].Width = groupBox4.Width - 20;
                    }
                    int q = 0;
                    foreach (testResult tr in helyesek)
                    {
                        foreach (concreteTest ct in tr.lista)
                        {

                            D.Add(double.Parse(ct.time.Substring(0,ct.time.Length-2), System.Globalization.CultureInfo.InvariantCulture));

                            LBs[q].Text = ct.ToString();
                            LBs[q].Visible = true;
                            q++;
                        }

                    }
                    double d = 0;
                    foreach (double t in D)
                    {
                        d = Math.Max(d, t);
                    }
                    q = 0;
                    foreach (double t in D)
                    {
                        PBs[q].Value = (int)(t * 100 / d);
                        PBs[q].Visible = true;
                        q++;
                    }


                }
                catch
                {
                }

            }
        }

    }
    public class concreteTest
    {
        //nem csak minepar-t, minden más szart is
        private static void berak(System.Windows.Forms.CheckedListBox.ObjectCollection minePar, string mparameter)
        {
            bool b = true;
            foreach (object o in minePar)
            {
                if ((string)o == mparameter)
                    b = false;
            }
            if (b)
                minePar.Add(mparameter,true);
        }
        public override string ToString()
        {
            return mineParameter + " " + mineType + " " + securityType + " " + threadingType + " " + time;
        }

        public static System.Windows.Forms.CheckedListBox.ObjectCollection mineParameters;
        public static System.Windows.Forms.CheckedListBox.ObjectCollection threadingTypes;
        public static System.Windows.Forms.CheckedListBox.ObjectCollection mineTypes;
        public static System.Windows.Forms.CheckedListBox.ObjectCollection securityTypes;
        public string mineParameter, threadingType, mineType, securityType;
        public List<string> data;
        public string time;
        public bool stimmel(string mP, string tT, string mT, string sT)
        {
            if (mP == mineParameter && tT == threadingType && mT == mineType && sT == securityType)
                return true;
            return false;
        }
        public concreteTest(StreamReader sr)
        {
            sr.ReadLine();
            mineParameter = sr.ReadLine().Split(' ')[0];
            mineParameter += " " + sr.ReadLine().Split(' ')[0];
            mineParameter += " " + sr.ReadLine().Split(' ')[0];
            mineParameter += " " + sr.ReadLine().Split(' ')[0];
            berak(mineParameters, mineParameter);
            threadingType = sr.ReadLine().Split(' ')[0];
            berak(threadingTypes, threadingType);
            mineType = sr.ReadLine().Split(' ')[0];
            berak(mineTypes, mineType);
            securityType = sr.ReadLine().Split(' ')[0];
            berak(securityTypes, securityType);
            data = new List<string>();
            sr.ReadLine();
            string line;
            while ((line = sr.ReadLine()).Substring(0, 2) != "to")
            {
                data.Add(line);
            }
            time = line.Split(' ')[2];
            sr.ReadLine();
            
        }
    }
    public class testResult
    {
        public List<concreteTest> lista;
        public string procName;
        public testResult(testResult tr)
        {
            lista = new List<concreteTest>();
            procName = tr.procName;
        }

        public testResult(StreamReader sr)
        {
            lista = new List<concreteTest>();
            procName = sr.ReadLine();
            while (!sr.EndOfStream)
                lista.Add(new concreteTest(sr));
        }
    }
}
